package com.xb.cloud.order.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.order.model.SoExample;
import com.xb.cloud.order.model.SoExample.Criteria;
import com.xb.cloud.order.model.mapper.SellOrderMapper;
import com.xb.cloud.order.service.client.FinanceService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

@Service
public class FinanceServiceImpl implements FinanceService {

	@Resource
	SellOrderMapper sellOrderMapper;

	@Override
	public ServiceReturnInfo getMerchantFinaceInfo(EndMerchantUserBo merchantUserBo) {
		Byte accountType = merchantUserBo.getAccountType();

		Long companyId = merchantUserBo.getCompanyId();
		Long merchantId = merchantUserBo.getMerchantId();
		Long shopId = merchantUserBo.getShopId();

		SoExample example = new SoExample();

		Criteria createCriteria = example.createCriteria();
		if (DbConstants.ACCOUNT_TYPE_TOP_MERCHANT == accountType
				|| DbConstants.ACCOUNT_TYPE_LEAF_MERCHANT == accountType) {
			// 商家用户
			createCriteria.andCompanyIdEqualTo(companyId).andMerchantIdEqualTo(merchantId);
		}

		if (DbConstants.ACCOUNT_TYPE_SHOP == accountType) {
			// 店铺用户
			createCriteria.andCompanyIdEqualTo(companyId).andMerchantIdEqualTo(merchantId).andShopIdEqualTo(shopId);
		}

		List<Integer> status = Arrays.asList(DbConstants.SO_STATUS_ALREADY_PAY,
				DbConstants.SO_STATUS_ALREADY_COMPLETED);
		createCriteria.andSoStatusIn(status);

		BigDecimal decimal = sellOrderMapper.getMerchantSellTotalAmountWithStatus(example);

		return ServiceReturnUtils.success(decimal);
	}

}
